<?php

namespace app\admin\validate;

use think\Validate;

class Pay extends Validate
{
    /**
     * 验证规则
     */
    protected $rule = [
        'ordersn'     => 'require|notEmpty',
        'price'       => 'require|float|notEmpty',
        'paytype'     => 'require|notEmpty',
        'callbackurl' => 'require|url|notEmpty',
        'payplatform' => 'require|notEmpty',
        'nonce_str'   => 'require|notEmpty',
        'openid'      => 'require|notEmpty',
        'title'       => 'require|notEmpty',
        'appid'       => 'require|notEmpty',
        'platform'       => 'require|notEmpty',
    ];

    /**
     * 字段描述
     */
    protected $field = [
    ];

    /**
     * 提示消息
     */
    protected $message = [
        'ordersn.require'     => '订单号不能为空',
        'price.require'       => '价格不能为空',
        'price.float'         => '价格必须是数字',
        'paytype.require'     => '支付类型不能为空',
        'callbackurl.require' => '回调地址不能为空',
        'callbackurl.url'     => '回调地址必须是有效的 URL',
        'payplatform.require' => '支付平台不能为空',
        'nonce_str.require'   => '随机字符串不能为空',
        'title.require'       => '商品名称不能为空',
        'platform.require'    => '平台不能为空'
    ];

    /**
     * 验证场景
     */
    protected $scene = [
        'add'  => ['ordersn', 'price', 'paytype', 'callbackurl', 'payplatform', 'nonce_str','title','platform'],
        'edit' => ['ordersn', 'price', 'paytype'], // 确保字段在 rule 中定义
    ];

    public function __construct(array $rules = [], $message = [], $field = [])
    {
        $this->field = [
            'username' => __('Username'),
            'nickname' => __('Nickname'),
            'password' => __('Password'),
            'email'    => __('Email'),
            'mobile'   => __('Mobile')
        ];

        // 注册自定义规则
        $this->extend('notEmpty', function ($value) {
            return !empty($value); // 确保值不为空
        });

        parent::__construct($rules, $message, $field);
    }
}